var e = require("../../../../@babel/runtime/helpers/defineProperty"), t = require("../../../../@babel/runtime/helpers/slicedToArray"), i = require("../../../uv-ui-tools/libs/mixin/mpMixin.js"), n = require("../../../uv-ui-tools/libs/mixin/mixin.js"), r = require("./props.js"), a = require("../../../uv-ui-tools/libs/util/dayjs.js"), s = require("../../../../common/vendor.js");

require("../../../uv-ui-tools/libs/function/index.js"), require("../../../uv-ui-tools/libs/function/test.js"), 
require("../../../uv-ui-tools/libs/function/digit.js"), require("../../../uv-ui-tools/libs/util/route.js"), 
require("../../../uv-ui-tools/libs/function/debounce.js"), require("../../../uv-ui-tools/libs/function/throttle.js");

var o = {
    name: "uv-datetime-picker",
    emits: [ "close", "cancel", "confirm", "input", "change", "update:modelValue" ],
    mixins: [ i.mpMixin, n.mixin, r.props ],
    data: function() {
        return {
            columns: [],
            innerDefaultIndex: [],
            innerFormatter: function(e, t) {
                return t;
            }
        };
    },
    watch: {
        propsChange: function() {
            this.init();
        }
    },
    computed: {
        propsChange: function() {
            var e = this.value || this.modelValue;
            return [ this.mode, this.maxDate, this.minDate, this.minHour, this.maxHour, this.minMinute, this.maxMinute, this.filter, e ];
        }
    },
    mounted: function() {
        this.init();
    },
    methods: {
        init: function() {
            this.getValue(), this.updateColumnValue(this.innerValue);
        },
        getValue: function() {
            var e = this.value || this.modelValue;
            this.innerValue = this.correctValue(e);
        },
        setFormatter: function(e) {
            this.innerFormatter = e;
        },
        open: function() {
            this.$refs.picker.open(), this.getValue(), this.updateColumnValue(this.innerValue);
        },
        close: function() {
            this.$emit("close");
        },
        cancel: function() {
            this.$emit("cancel");
        },
        confirm: function() {
            this.$emit("confirm", {
                value: this.innerValue,
                mode: this.mode
            }), this.clearDate || (this.$emit("input", this.innerValue), this.$emit("update:modelValue", this.innerValue));
        },
        intercept: function(e, t) {
            var i = e.match(/\d+/g);
            return i.length > 1 ? (this.$uv.error("请勿在过滤或格式化函数时添加数字"), 0) : t && 4 == i[0].length ? i[0] : i[0].length > 2 ? (this.$uv.error("请勿在过滤或格式化函数时添加数字"), 
            0) : i[0];
        },
        change: function(e) {
            var t = e.indexs, i = e.values, n = "";
            if ("time" === this.mode) n = "".concat(this.intercept(i[0][t[0]]), ":").concat(this.intercept(i[1][t[1]])); else if ("year" === this.mode) {
                var r = parseInt(this.intercept(i[0][t[0]], "year"));
                n = Number(new Date(r, 0));
            } else {
                var s = parseInt(this.intercept(i[0][t[0]], "year")), o = parseInt(this.intercept(i[1][t[1]])), u = parseInt(i[2] ? this.intercept(i[2][t[2]]) : 1), c = 0, h = 0, m = a.dayjs("".concat(s, "-").concat(o)).daysInMonth();
                "year-month" === this.mode && (u = 1), u = Math.min(m, u), "datetime" === this.mode && (c = parseInt(this.intercept(i[3][t[3]])), 
                h = parseInt(this.intercept(i[4][t[4]]))), n = Number(new Date(s, o - 1, u, c, h));
            }
            n = this.correctValue(n), this.innerValue = n, this.updateColumnValue(n), this.$emit("change", {
                value: n,
                mode: this.mode
            });
        },
        updateColumnValue: function(e) {
            this.innerValue = e, this.updateColumns(), this.updateIndexs(e);
        },
        updateIndexs: function(e) {
            var t = this, i = [], n = this.formatter || this.innerFormatter;
            if ("time" === this.mode) {
                var r = e.split(":");
                i = [ n("hour", r[0]), n("minute", r[1]) ];
            } else i = [ n("year", "".concat(a.dayjs(e).year())), n("month", this.$uv.padZero(a.dayjs(e).month() + 1)) ], 
            "date" === this.mode && i.push(n("day", this.$uv.padZero(a.dayjs(e).date()))), "datetime" === this.mode && i.push(n("day", this.$uv.padZero(a.dayjs(e).date())), n("hour", this.$uv.padZero(a.dayjs(e).hour())), n("minute", this.$uv.padZero(a.dayjs(e).minute())));
            var s = this.columns.map(function(e, t) {
                return Math.max(0, e.findIndex(function(e) {
                    return e === i[t];
                }));
            });
            this.$nextTick(function() {
                t.$uv.sleep(100).then(function(e) {
                    t.$refs.picker.setIndexs(s, !0);
                });
            });
        },
        updateColumns: function() {
            var e = this.formatter || this.innerFormatter, t = this.getOriginColumns().map(function(t) {
                return t.values.map(function(i) {
                    return e(t.type, i);
                });
            });
            this.columns = t;
        },
        getOriginColumns: function() {
            var e = this;
            return this.getRanges().map(function(t) {
                var i = t.type, n = t.range, r = function(e, t) {
                    for (var i = -1, n = Array(e < 0 ? 0 : e); ++i < e; ) n[i] = t(i);
                    return n;
                }(n[1] - n[0] + 1, function(t) {
                    var r = n[0] + t;
                    return r = "year" === i ? "".concat(r) : e.$uv.padZero(r);
                });
                return e.filter && (r = e.filter(i, r)), {
                    type: i,
                    values: r
                };
            });
        },
        generateArray: function(e, t) {
            return Array.from(new Array(t + 1).keys()).slice(e);
        },
        correctValue: function(e) {
            var i = "time" !== this.mode;
            if (i && !this.$uv.test.date(e) ? e = this.minDate : i || e || (e = "".concat(this.$uv.padZero(this.minHour), ":").concat(this.$uv.padZero(this.minMinute))), 
            i) return e = a.dayjs(e).isBefore(a.dayjs(this.minDate)) ? this.minDate : e, a.dayjs(e).isAfter(a.dayjs(this.maxDate)) ? this.maxDate : e;
            if (-1 === String(e).indexOf(":")) return this.$uv.error("时间错误，请传递如12:24的格式");
            var n = e.split(":"), r = t(n, 2), s = r[0], o = r[1];
            return s = this.$uv.padZero(this.$uv.range(this.minHour, this.maxHour, Number(s))), 
            o = this.$uv.padZero(this.$uv.range(this.minMinute, this.maxMinute, Number(o))), 
            "".concat(s, ":").concat(o);
        },
        getRanges: function() {
            if ("time" === this.mode) return [ {
                type: "hour",
                range: [ this.minHour, this.maxHour ]
            }, {
                type: "minute",
                range: [ this.minMinute, this.maxMinute ]
            } ];
            var e = this.getBoundary("max", this.innerValue), t = e.maxYear, i = e.maxDate, n = e.maxMonth, r = e.maxHour, a = e.maxMinute, s = this.getBoundary("min", this.innerValue), o = s.minYear, u = s.minDate, c = [ {
                type: "year",
                range: [ o, t ]
            }, {
                type: "month",
                range: [ s.minMonth, n ]
            }, {
                type: "day",
                range: [ u, i ]
            }, {
                type: "hour",
                range: [ s.minHour, r ]
            }, {
                type: "minute",
                range: [ s.minMinute, a ]
            } ];
            return "date" === this.mode && c.splice(3, 2), "year-month" === this.mode && c.splice(2, 3), 
            "year" === this.mode && c.splice(1, 4), c;
        },
        getBoundary: function(t, i) {
            var n = new Date(i), r = new Date(this["".concat(t, "Date")]), s = a.dayjs(r).year(), o = 1, u = 1, c = 0, h = 0;
            return "max" === t && (o = 12, u = a.dayjs(n).daysInMonth(), c = 23, h = 59), a.dayjs(n).year() === s && (o = a.dayjs(r).month() + 1, 
            a.dayjs(n).month() + 1 === o && (u = a.dayjs(r).date(), a.dayjs(n).date() === u && (c = a.dayjs(r).hour(), 
            a.dayjs(n).hour() === c && (h = a.dayjs(r).minute())))), e(e(e(e(e({}, "".concat(t, "Year"), s), "".concat(t, "Month"), o), "".concat(t, "Date"), u), "".concat(t, "Hour"), c), "".concat(t, "Minute"), h);
        }
    }
};

Array || s.resolveComponent("uv-picker")(), Math;

var u = s._export_sfc(o, [ [ "render", function(e, t, i, n, r, a) {
    return {
        a: s.sr("picker", "585cecf8-0"),
        b: s.o(a.close),
        c: s.o(a.cancel),
        d: s.o(a.confirm),
        e: s.o(a.change),
        f: s.p({
            closeOnClickOverlay: e.closeOnClickOverlay,
            closeOnClickConfirm: e.closeOnClickConfirm,
            columns: r.columns,
            title: e.title,
            itemHeight: e.itemHeight,
            showToolbar: e.showToolbar,
            visibleItemCount: e.visibleItemCount,
            defaultIndex: r.innerDefaultIndex,
            cancelText: e.cancelText,
            confirmText: e.confirmText,
            cancelColor: e.cancelColor,
            confirmColor: e.confirmColor,
            round: e.round
        })
    };
} ] ]);

wx.createComponent(u);